package com.kaiyidiannao.model.req.model;

import android.util.Log;
import com.kaiyidiannao.data.DataCenter;
import com.kaiyidiannao.https.HttpUtils;
import com.kaiyidiannao.model.Result;
import com.kaiyidiannao.model.bean.MessageBean;
import com.kaiyidiannao.model.req.base.BaseCommand;
import com.taplinker.core.util.json.JsonUtil;
import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

/**
 *   玩乐住, 美食街,石子街 公用的请求数据-搜索
 */
public class PostMyMessageInfo extends BaseCommand {

    private int catid; //当前分类编号
    private String title; //搜索关键词
    private int page = DataCenter.LISTVIEW_DEFAULT_PAGE;

    private String TAG = "GetMyPublishSmall";

    private String userid;
    private String mid;

    public PostMyMessageInfo(int catid, String userid, String mid, int page) {
        this.catid = catid;
        this.userid = userid;
        this.mid = mid;
        this.page = page;
    }

    public PostMyMessageInfo(int catid, String userid, String mid) {
        this.catid = catid;
        this.userid = userid;
        this.mid = mid;
        this.page = page;
    }

    @Override
    public String getRequestUrl() {
        return DataCenter.GET_MY_PUBLISH;
    }

    @Override
    public HashMap<String, String> getRequestParamters() {
        HashMap<String, String> paramters = new HashMap<String, String>();
        paramters.put("catid",catid+"");
        paramters.put("userid", userid);
        paramters.put("mid", mid);
        if(!"".equals(page)&&null!=page+""){
            paramters.put("page",page + "");
        }
        return paramters;
    }

    @Override
    public Result go(String url, HashMap<String, String> paramters) { // 联网时后台执行
        // 根据请求参数返回请求数据
        return HttpUtils.postByHttpClient(url, paramters);
    }

    @Override
    public void yes(Result result) { //后台执行完毕后执行
        super.yes(result);
        Log.d(DataCenter.LOG_INFO, "GetMyMessagePublish.yes()");

        try {
            Log.d(TAG, result.object.toString());
            JSONObject jsonObject = new JSONObject(result.object.toString());
            String code = jsonObject.getString("code");
            String msg = jsonObject.getString("msg");
            JSONArray  jsonArray = jsonObject.optJSONArray("data");
            if (page == DataCenter.PAGE) {
                Log.d("-=-=-","-=-=-");
                for (int i = DataCenter.messageInfoList.size() - 1; i >= (DataCenter.mypub_position / 20 + 1) * 20 - 20; i--) {
                    DataCenter.messageInfoList.remove(i);
                }
            }
            ArrayList<MessageBean> targetList = DataCenter.messageInfoList;
            if(page == DataCenter.LISTVIEW_DEFAULT_PAGE) {
                targetList.clear();
            }
            for(int i = 0; i < jsonArray.length(); ++i){ // i从1开始，屏蔽提示信息
                JSONObject o = jsonArray.getJSONObject(i);
                MessageBean bean = JsonUtil.jsonToObject(o.toString(), MessageBean.class);
                targetList.add(bean);
            }
            DataCenter.code = code.toString();
            DataCenter.msg = msg.toString();
            Log.d(TAG, targetList.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_MY_PUBLISH_SUCCESS);
    }

    @Override
    public void no(Result result) {
        super.no(result);
        DataCenter.getInstance().callBacks(DataCenter.flag.GET_MY_PUBLISH_FAILED);
    }
}
